package ouyuwang.gpsbox;

import android.app.Activity;
import android.content.Context;
import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;


/**
 * Created by yupeng on 17/07/19.
 */

public class FileUtil {

    private static FileUtil FILEUTIL = null;

    private FileUtil() {
    }

    private static File FILE = null;
    private static FileOutputStream FOS = null;
    private static final String FOLDER_NAME = "AAA";

    public static void clearEmptyFiles() {
        String filePath = null;
        boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if (hasSDCard) { // SD卡根目录
            filePath = Environment.getExternalStorageDirectory().toString() + File.separator +
                    FOLDER_NAME;
        } else  // 系统下载缓存根目录
            filePath = Environment.getDownloadCacheDirectory().toString() + File.separator + FOLDER_NAME;
        try {
            File file = new File(filePath);
            if (file!=null && file.exists() && file.isDirectory()) {
                for(File f:file.listFiles()){
                    if(f.length()<1){
                        f.delete();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static FileUtil init(String fileName) {
        if (FILEUTIL == null || fileName != null) {
            String name = fileName == null ? "NULL" : fileName;
            FILEUTIL = new FileUtil();
            String filePath = null;
            boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
            if (hasSDCard) { // SD卡根目录的hello.text
                filePath = Environment.getExternalStorageDirectory().toString() + File.separator +
                        FOLDER_NAME + File.separator + name;
            } else  // 系统下载缓存根目录的hello.text
                filePath = Environment.getDownloadCacheDirectory().toString() + File.separator + FOLDER_NAME +
                        File.separator + name;
            try {
                File file = new File(filePath);
                if (!file.exists()) {
                    File dir = new File(file.getParent());
                    dir.mkdirs();
                    file.createNewFile();
                }
                FILEUTIL.FILE = file;
                if (FOS != null) {
                    FOS.close();
                    FOS = null;
                }
                FOS = new FileOutputStream(file, true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return FILEUTIL;
    }

    public void write(String message) {
        message += "\n";
        try {
            FOS.write(message.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void close() {
        try {
            if (FOS != null) {
                FOS.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            FOS = null;
        }
    }

}
